layui.define(['layer','form'],function(exports){
	var $ = layui.$, layer = layui.layer, form = layui.form;
	var obj = {
		/*@列表*/
		form: {

			/*@获取子级分类ajax*/
			sun_cate: function(url, objData, field, ty){
				
				$.post(url, objData, function(data){
					console.log(data)
					if(data.code !== 1){
						layer.msg(data.msg, {icon: 2, time: 1000})
						return
					}
					var html = '';
					if(ty == 0){
						html += '<option value="">全部</option>';
						if(data.msg.length > 0){
							for(var i = 0;i < data.msg.length;++i){
								html += '<option value="'+data.msg[i].category_id+'">'+data.msg[i].category_name+'</option>';
							}
						}
						$('select[name="'+field+'"]').html(html)
					}else if(ty == 1){
						for(var j = 0;j < field.length;++j){
							console.log(field[j])
							html = '';
							var v = data.msg[field[j]]
							html += '<option value="">全部</option>';
							if(v.length > 0){
								for(var i = 0;i < v.length;++i){
									html += '<option value="'+v[i].attribute_name+'">'+v[i].attribute_name+'</option>';
								}
							}
							$('select[name="'+field[j]+'"]').html(html)
						}
					}
					
					form.render("select");
				})
			},
			/*@select选择监听*/
			g_cate: function(select_name, url){
				form.on('select('+select_name+')',function(data){
					var name = $(data.elem).attr('name')
					console.log(name)
					console.log(url)
					if(name != 'category_id' && name != 'cat_id'){
						return
					}
					if(name == 'category_id'){
						$('select[name="cat_id"]').html('')
						field = 'cat_id'
						ty = 0
					}else if(name == 'cat_id'){
						field = ['brand_name','memory_name','attribute_name','colour_name']
						ty = 1
					}
					$('select[name="brand_name"]').html('')
					$('select[name="memory_name"]').html('')
					$('select[name="attribute_name"]').html('')
					$('select[name="colour_name"]').html('')

					if(data.value != ''){
						objData = {
							fid: data.value,
							ty: ty
						}
						obj.form.sun_cate(url, objData, field, ty)
					}else{
						form.render("select");
					}
				})
			},
			
		},
	};
	//输出接口
	exports('gcate', obj);
})